package com.bluegalaxy.nhb.snservice.core.util;

import com.bluegalaxy.nhb.snservice.common.utils.DateUtil;
import org.jsoup.helper.DataUtil;

import java.security.MessageDigest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 采用MD5加密解密
 * @author sublun
 *
 */
public class MD5 {  
  
    /*** 
     * MD5加码 生成32位md5码 
     */  
    public static String string2MD5(String inStr){  
        MessageDigest md5 = null;  
        try{  
            md5 = MessageDigest.getInstance("MD5");  
        }catch (Exception e){
            System.out.println(e.toString());
            e.printStackTrace();  
            return "";  
        }  
        char[] charArray = inStr.toCharArray();  
        byte[] byteArray = new byte[charArray.length];  
  
        for (int i = 0; i < charArray.length; i++)  
            byteArray[i] = (byte) charArray[i];  
        byte[] md5Bytes = md5.digest(byteArray);  
        StringBuffer hexValue = new StringBuffer();  
        for (int i = 0; i < md5Bytes.length; i++){  
            int val = ((int) md5Bytes[i]) & 0xff;  
            if (val < 16)  
                hexValue.append("0");  
            hexValue.append(Integer.toHexString(val));  
        }  
        return hexValue.toString();  
  
    }  
  
    /** 
     * 加密解密算法 执行一次加密，两次解密 
     */   
    public static String convertMD5(String inStr){  
  
        char[] a = inStr.toCharArray();  
        for (int i = 0; i < a.length; i++){  
            a[i] = (char) (a[i] ^ 't');  
        }  
        String s = new String(a);  
        return s;  
  
    }  
    
    /**
     * MD5方法
     * 
     * @param text 明文
     * @param key 密钥
     * @return 密文
     * @throws Exception
     */
	public static String md5(String text, String key) throws Exception {
		byte[] bytes = (text + key).getBytes("utf-8");
		
		MessageDigest messageDigest = MessageDigest.getInstance("MD5");
		messageDigest.update(bytes);
		bytes = messageDigest.digest();
		
		StringBuilder sb = new StringBuilder();
		for(int i = 0; i < bytes.length; i ++)
		{
			if((bytes[i] & 0xff) < 0x10)
			{
				sb.append("0");
			}

			sb.append(Long.toString(bytes[i] & 0xff, 16));
		}
		
		return sb.toString().toLowerCase();
	}

    // 测试主函数
    public static void main(String args[]) {
//        String s = new String("2021-05-01");
        String s = "";
        try {
            s = "HDAA"+(DateUtil.parseDate(DateUtil.DATE_yyyyMMdd,"20210401").getTime()+"")+"AAFLOW";
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println("原始：" + s);
        try {
            System.out.println("MD5后：" + string2MD5(s));
        } catch (Exception e) {
            e.printStackTrace();
        }
//        s =string2MD5(s);
        System.out.println("加密的：" + convertMD5(s));
        System.out.println("解密的：" + convertMD5(convertMD5(s)));
        System.out.println("解密的：" + convertMD5("<055EBDM@GD@DDDDD5528;#"));

        System.out.println("解密的时间：" + timeStamp2Date("1609430400000",""));


    }

    public static String timeStamp2Date(String seconds,String format) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

        String sd = sdf.format(new Date(Long.parseLong(seconds)));

        System.out.println(sd);

        return sd;
    }

}